package com.topvision.bureau.presenter;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.kernal.demo.plateid.R;
import com.kernal.plateid.MemoryCameraActivity;
import com.kernal.plateid.PermissionActivity;
import com.kernal.plateid.RecogService;
import com.topvision.bureau.model.regmode.IRegMode;
import com.topvision.bureau.model.regmode.OnRegListener;
import com.topvision.bureau.model.regmode.RegMode;
import com.topvision.bureau.model.impl.RegModeImpl;
import com.topvision.bureau.view.regmode.ChooseCarModeActivity;
import com.topvision.bureau.view.regmode.IRegModeView;
import com.topvision.face.ChooseFaceModeActivity;
import com.topvision.face.FRActivity;
import com.topvision.idcard.CameraActivity;

import java.util.List;

import utills.CheckPermission;


/**
 * Created by gushenjie on 17/6/18.
 */

public class RegModePresenter implements OnRegListener {
    //人脸实时模式
    public static final String FACE_REAL_TIME_FLAG = "FACE_REAL_TIME_FLAG";
    public static final String REG_MODE = "reg_mode";
    public static final int REG_MODE_FACE = 1;
    public static final int REG_MODE_BANK_CARD = 2;
    public static final int REG_MODE_IDENTITY = 3;

    private Context context;
    private IRegMode iRegMode;
    private IRegModeView iRegModeView;
    static final String[] PERMISSION = new String[]{Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,// 写入权限
            Manifest.permission.READ_EXTERNAL_STORAGE, // 读取权限
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.VIBRATE, Manifest.permission.INTERNET,
            Manifest.permission.FLASHLIGHT};

    public RegModePresenter(Context context) {
        this.context = context;
    }

    public RegModePresenter(Context context, IRegModeView iRegModeView) {
        this.context = context;
        iRegMode = new RegModeImpl();
        this.iRegModeView = iRegModeView;
    }

    public void getRegModes() {
        iRegMode.getRegModels(context, this);
    }

    @Override
    public void onSuccess(List<RegMode> regModeList) {
        iRegModeView.showRegModesView(regModeList);
    }

    @Override
    public void onFailure() {
        iRegModeView.showRegModesView(null);
    }


    public void forwardRegMode(Activity mActivity, RegMode regMode) {
        //人脸
        if (regMode.getRegName().equals(context.getString(R.string.face_reg))) {
            forwardChooseFaceMode();
        }
        //车牌
        else if (regMode.getRegName().equals(context.getString(R.string.car_reg))) {
            forwardChooseCarMode();
        }
        //银行卡
        else if (regMode.getRegName().equals(context.getString(R.string.bank_card_reg))) {
                forwardBankCardMode(CameraActivity.TYPE_BANK_CAR);
        }
        //身份证
        else if (regMode.getRegName().equals(context.getString(R.string.identity_reg))) {
            forwardBankCardMode(CameraActivity.TYPE_ORC);
        }
    }

    /**
     * 跳转选择人脸模式类型
     */
    public void forwardChooseFaceMode(){
        Intent chooseModeIntent = new Intent(context, ChooseFaceModeActivity.class);
        context.startActivity(chooseModeIntent);
    }

    /**
     * 跳转到车牌识别
     */
    public void forwardChooseCarMode(){
        Intent chooseModeIntent = new Intent(context, ChooseCarModeActivity.class);
        context.startActivity(chooseModeIntent);
    }


    /**
     * 跳转到银行卡 or 身份证 识别
     */
    public void forwardBankCardMode(String type){
        Intent bankIdentityIntent = new Intent(context, CameraActivity.class);
        bankIdentityIntent.putExtra(CameraActivity.TYPE_MODE,type);
        context.startActivity(bankIdentityIntent);
    }




    /*跳转到视频扫描识别界面
    *@Params : [activity, recogModel]  recogModel == true 精准 false 极速
    *@Author : gushenjie
    *@Date :17/6/18
    */
    public void jumpVideoRecog(boolean recogModel, Activity activity) {
        Intent video_intent = new Intent();
        video_intent.putExtra("camera", true);
        RecogService.recogModel = recogModel;//true  精准模式 多帧识别  false:快速模式  单帧识别
        video_intent = new Intent(context, MemoryCameraActivity.class);
        if (Build.VERSION.SDK_INT >= 23) {
            CheckPermission checkPermission = new CheckPermission(context);
            if (checkPermission.permissionSet(PERMISSION)) {
                PermissionActivity.startActivityForResult(activity, 0, "true", PERMISSION);
            } else {
                video_intent.setClass(context, MemoryCameraActivity.class);
                video_intent.putExtra("camera", true);
                context.startActivity(video_intent);

            }
        } else {
            video_intent.setClass(context, MemoryCameraActivity.class);
            video_intent.putExtra("camera", true);
            context.startActivity(video_intent);

        }
    }


    //跳转到拍照识别界面
    public void jumpTakePic(Activity activity) {
        Intent cameraIntent = new Intent(context, MemoryCameraActivity.class);
        if (Build.VERSION.SDK_INT >= 23) {
            CheckPermission checkPermission = new CheckPermission(context);
            if (checkPermission.permissionSet(PERMISSION)) {
                PermissionActivity.startActivityForResult(activity, 0, "false", PERMISSION);
            } else {
                cameraIntent.putExtra("camera", false);
                context.startActivity(cameraIntent);

            }
        } else {
            cameraIntent.putExtra("camera", false);
            context.startActivity(cameraIntent);

        }
    }


    //跳转到拍照识别界面
    public void jumpFace(boolean realTime) {
        Intent faceIntent = new Intent(context, FRActivity.class);
       // faceIntent.putExtra(REG_MODE,regMode);
        faceIntent.putExtra(FACE_REAL_TIME_FLAG, realTime);
        context.startActivity(faceIntent);
    }
}
